package com.GZX;

import com.publicClass.User;
import com.util.Vessel;
import com.util.file.Student;

import static com.util.Vessel.idCards;
import static com.util.Vessel.scanner;

import java.util.*;

public class idCard {

    private long id;
    private int cId;   //校园卡号

    private double balance;  //校园卡余额

    private cardStatus cStatus;//校园卡状态

    public idCard() {
    }

    public idCard(long id, int cId, double balance, cardStatus cStatus) {
        this.id = id;
        this.cId = cId;
        this.balance = balance;
        this.cStatus = cStatus;
    }

    public idCard(int cId, double balance, cardStatus cStatus) {
        this.cId = cId;
        this.balance = balance;
        this.cStatus = cStatus;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getcId() {
        return cId;
    }

    public void setcId(int cId) {
        this.cId = cId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public cardStatus getcStatus() {
        return cStatus;
    }

    public void setcStatus(cardStatus cStatus) {
        this.cStatus = cStatus;
    }

    public String toString(){
        return getId() + "\t\t\t"+ getcId() + "\t\t\t"  + getBalance() + "\t\t\t" + getcStatus();
    }

    //新办
    public static void addCard(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号");
        long l = scanner.nextLong();
        for (User user: Vessel.users) {
            if (l == user.getId()){
                int newCid = Vessel.cId++;
                Vessel.balance = 0;
                cardStatus cardStatus = null;
                cardStatus=cardStatus.正常;
                idCard newIdcard = new idCard(l,newCid,Vessel.balance,cardStatus);
                idCards.add(newIdcard);
                System.out.println("办理成功！");
                for (idCard idCard : idCards ) {
                    if (idCard.getId() == l){
                        System.out.println(idCard.toString());
                    }
                }
                break;
            }
        }
    }

    //充值
    public static void recharge(){
        System.out.println("请输入校园卡号：");
        int cId = scanner.nextInt();
        for (idCard card : idCards){
            if (card.getcId() == cId){
                System.out.println("请输入充值金额");
                double cBalance = scanner.nextDouble();
                card.setBalance(cBalance);
                System.out.println("充值完成");
                for (int i = 0; i < idCards.size(); i++) {
                    if (card.getcId() == cId){
                        System.out.println(idCards.get(i).toString());
                    }
                }
            }
        }
    }

    //挂失
    public static void reportLoss(){
        System.out.println("请输入校园卡号：");
        int cId = scanner.nextInt();
        for (idCard card : idCards){
            if (card.getcId() == cId){
                card.setcStatus(cardStatus.挂失);
                System.out.println("已挂失");
                for (int i = 0; i < idCards.size(); i++) {
                    if (card.getcId() == cId){
                        System.out.println(idCards.get(i).toString());
                    }
                }
            }
        }
    }

    //取消挂失
    public static void cancelLoss(){
        System.out.println("请输入校园卡号：");
        int cId = scanner.nextInt();
        for (idCard card : idCards){
            if (card.getcId() == cId){
                card.setcStatus(cardStatus.正常);
                System.out.println("已取消挂失");
                for (int i = 0; i < idCards.size(); i++) {
                    if (card.getcId() == cId){
                        System.out.println(idCards.get(i).toString());
                    }
                }
            }
        }
    }

    //补办
    public static void replace(){
        System.out.println("请输入丢失的校园卡号");
        int cId = scanner.nextInt();
        long id = 0;
        idCard newIdcard1 = new idCard();
        for (idCard idcard : idCards){
            if (idcard.getcId() == cId && idcard.getcStatus().equals(cardStatus.挂失)){
                idcard.setcStatus(cardStatus.注销);
                id = idcard.getId();
                int newCid = Vessel.cId++;
                Vessel.balance = idcard.balance;
                idcard.setBalance(0);
                cardStatus cardStatus = null;
                cardStatus=cardStatus.正常;
                idCard newIdcard = new idCard(id,newCid,Vessel.balance,cardStatus);
                newIdcard1 = newIdcard;


            }
        }
        idCards.add(newIdcard1);
        System.out.println("补办成功");
        for (int i = 0; i < idCards.size(); i++) {
            if (idCards.get(i).getId() == id){
                System.out.println(idCards.get(i).toString());
            }
        }
    }

    //消费模拟
    public static void consume(){
        Random random = new Random();
        System.out.println("请输入要消费的卡号：");
        double inputConsume = scanner.nextDouble();
        for (idCard card : idCards){
            if (card.getcId() == inputConsume){
                double money = card.getBalance();
                money -= random.nextInt( 10 );
                card.setBalance(money);
            }
        }
    }

    //学生校园卡
    public static void studentCard(){
        System.out.println("请选择你的操作：1：校园卡充值  2：校园卡挂失  3：校园卡取消挂失");
        int choice = scanner.nextInt();
        switch (choice){
            case 1:
                recharge();
                break;
            case 2:
                reportLoss();
                break;
            case 3:
                cancelLoss();
                break;
        }
    }

    //管理员管理校园卡
    public static void manageCard(){
        System.out.println("请选择您的操作：1：激活校园卡 2：挂失校园卡 3：取消挂失 4 补办校园卡");
        int i = scanner.nextInt();
        switch (i){
            case 1:
                addCard();
                break;
            case 2:
                reportLoss();
                break;
            case 3:
                cancelLoss();
                break;
            case 4:
                replace();
                break;
        }
    }

    //校园卡信息查看
    public String idCardToString(){
        return getId()+"\t\t\t"+getBalance()+"\t\t\t"+getcStatus();
    }

    public static void main(String[] args) {
        studentCard();
    }

}
